package com.example.student.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.constants.RdsConstants;
import com.example.common.domain.pojo.ClassStudent;
import com.example.common.domain.pojo.Classes;
import com.example.common.domain.pojo.Student;
import com.example.common.utils.CacheClient;
import com.example.student.mapper.ClassesMapper;
import com.example.student.mapper.StudentMapper;
import com.example.student.service.ClassStudentService;
import com.example.student.service.ClassesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Service
public class ClassesServiceImpl extends ServiceImpl<ClassesMapper, Classes> implements ClassesService {

    @Autowired
    private CacheClient cacheClient;
    @Autowired
    private ClassStudentService classStudentService;
    @Autowired
    private StudentMapper studentMapper;

    @Override
    public Classes getClassesById(String id) {
        return cacheClient.queryWithPassThrough(
                RdsConstants.CLASSES_INFO_KEY,
                id,
                Classes.class,
                this::getById,
                RdsConstants.CACHE_TTL,
                TimeUnit.MINUTES,
                RdsConstants.CACHE_NULL_TTL,
                TimeUnit.MINUTES
        );
    }

    @Override
    public List<Classes> getClassesByIds(List<String> ids) {
        return super.baseMapper.selectBatchIds(ids);
    }

    @Override
    @Transactional
    public Classes getClassesByStudentId(String studentId) {
        LambdaQueryWrapper<ClassStudent> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ClassStudent::getStudentId, studentId);
        ClassStudent classStudent = classStudentService.getOne(queryWrapper);
        return getById(classStudent.getClassId());
    }

    @Override
    @Transactional(readOnly = true)
    public Page<Student> getClassesStudentPage(String classId, Integer pageNum, Integer pageSize) {
        // 参数校验，避免空指针
        if (classId == null || pageNum == null || pageSize == null) {
            return new Page<>(0, 0); // 返回空分页对象
        }

        // 查询班级-学生关联表，获取该班级下的所有学生ID
        LambdaQueryWrapper<ClassStudent> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ClassStudent::getClassId, classId);
        List<ClassStudent> classStudentList = classStudentService.list(queryWrapper);

        // 提取学生ID列表
        List<String> studentIds = classStudentList.stream()
                .map(ClassStudent::getStudentId)
                .filter(Objects::nonNull)
                .collect(Collectors.toList());

        // 构建分页对象
        Page<Student> page = new Page<>(pageNum, pageSize);

        // 如果学生ID列表为空，直接返回空分页
        if (studentIds.isEmpty()) {
            return page;
        }
        // 使用学生ID列表分页查询学生信息
        LambdaQueryWrapper<Student> studentQueryWrapper = new LambdaQueryWrapper<>();
        studentQueryWrapper.in(Student::getId, studentIds);
        return studentMapper.selectPage(page, studentQueryWrapper);
    }
}
